package com.atguigu.gulimall.order.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/26 23:19
 */
@Slf4j
@Component
public class AlipayTradeClose {
    
    public boolean closeTrade(String orderSn) {
        //https://openapi.alipay.com/gateway.do
        //https://openapi-sandbox.dl.alipaydev.com/gateway.do
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do","9021000137629127","MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCEoxxHmyFUNh9iHrhT0N5BBBpPJoOLsOYBJqThq42nF+YFUovWKjcmpZYlwT1WdRrngR9nJ3IfkS/nWfBXCmCi8hFGucCJUZMTil3XTs6RPQl/Xrle5g3Oj13sgWspvRqnoFYjytTDUhDcbOoMgwkEyhhJsmOzyHdPT2aDSuRELKJpQp7kc1X2vlSWOuSh50AIHpyP6a7Z8W+tduc1lfO9B1sW/84ylsjkHYNYvXgcBcfMHZkPITQWvv748XFSxxtar9x7eC8LG/hsBagnkRzqEk5QsRurlIUgx81x0TKZ387gbJIZluA9Nl1LtPrKmT61jmFcI9P8gMLqz273r3n7AgMBAAECggEAKXPYqMBg4DKyCDZVoUxLycIAfvAoeIXmvEFZknXNJYWgi9VKgrpa1ic2D8Ke6uBacjO2nsSDND5p3EINjQZfQU2p/JKdC82Ey7yYq2ujv1x2FWqDQJdhhUblMb8YwMirBexkP3dqm0iQoHMhipuBPA/1G0M3Sb7DGvRTs4C6aluYHHuFCjChtdeStxXOPyWB+Ua2HE3i5XLPtsbjIpKTcr5WlCaXCBwq1GFM9ZrhLqVVRmPp1A9KgrQYa2ZoBiOpNAjMv7Zyl0dyBTgDXW7+m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QTREcwGBT7DCTRVe5GBy21KDCnFyYyFFQHstC4ErNJtkhV73vem4IKeVkfiQGw2TBqQsYjyMyFfsq7/rY0qe5DBskqtvfupJH99Z6rQmOPyc8O60+mjxdd70ZWic/kpeKqXCBroWENGcXVbWZcUvBAKi1gA2bMUvuLaOtEtpGzRzgC9Ox1HCXTo0+pooVahqHLb9TObL9CBJX50PAqUHB+19Qj1B66yTcYKQAG1NP720L82H9dtsiUkfHhmb9WuwUnL+AtrwIDAQAB","RSA2");
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderSn);
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            log.error("closeTrade alipayClient request field exception", e);
        }
        if(response.isSuccess()){
            log.info("closeTrade request success");
            return true;
        } else {
            log.info("closeTrade request field");
            return false;
        }
    }
    
}
